<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="暂无搜索">
                  <el-input style="width: 70%;" />
                </el-form-item>
              </el-col>

            </el-row>
          </div>
          <div class="btnBox">
            <el-button class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn" v-has="'genericCategoryInfo_add'">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1" align="center"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column align="center" field="code" title="分类编码" min-width="80" />
          <vxe-column align="center" title="分类名称">
            <template v-slot="{ row }">
              <span>{{ row.name }}</span>
            </template>
          </vxe-column>
          <vxe-column align="center" title="操作" v-if="$showFn(['genericCategoryInfo_edit', 'genericCategoryInfo_del'])">
            <template v-slot="{ row }">
              <el-button v-if="row.value != '7'" type="text" size="big" v-has="'genericCategoryInfo_edit'"
                style="margin-right: 10px; color: rgb(0, 132, 255)" @click.stop="edit(row)">编辑</el-button>
              <el-button type="text" size="big" v-has="'genericCategoryInfo_del'" v-if="row.value != '7'"
                style="margin-right: 10px; color: red;" @click="$delFn('非标分类', del, row)">删除</el-button>
              <span v-if="row.value == '7'">
                -
              </span>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog" :width="width" :title="title" @btnOk="btnOk"
      @close="close">
      <el-form ref="addFormRef" :disabled="title === '详情'" :model="addForm" :rules="addRules" label-width="100px">
        <el-row :gutter="24">
          <el-col :span="22">
            <el-form-item label="分类名称" style="width: 100%" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="分类编码" v-if="title === '新增'" style="width: 100%" prop="code">
              <el-input v-model="addForm.code" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </AddDialog>
  </div>
</template>

<script>
import AddDialog from "@/components/Goods/AddDialog";
import { getGenericGoodsTypeList, updateGenericGoodsType, addGenericGoodsType, deletetGenericGoodsType } from '@/api/genericGoods'
export default {
  name: "MenuInfo",
  components: {
    AddDialog,
  },
  data() {
    return {
      width: "30%",
      title: "编辑",
      showDialog: false,
      addForm: {
        name: "", // 菜单名称
        key: '',
      },
      addRules: {
        name: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "change",
          },
          {
            min: 1,
            max: 50,
            message: "长度在1到50个字符",
            trigger: "change",
          },
        ],
        code: [
          {
            required: true,
            message: "请输入分类编码",
            trigger: "change",
          },
          {
            pattern: /^([A-Z])+$/,
            message: "只能输入大写字母",
            trigger: "change",
          },
          {
            min: 1,
            max: 50,
            message: "长度在1到50个字符",
            trigger: "change",
          },
        ]
      },
      categoryList: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      categoryId: "",
    };
  },
  created() {
    this.getGenericGoodsTypeList();
  },
  methods: {
    // 获取菜单列表
    async getGenericGoodsTypeList() {
      const res = await getGenericGoodsTypeList({ ...this.page });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.categoryList = result.list;
        console.log(result, "data");
        // this.page.pageSize = result.pageSize
        this.page.pages = result.pages;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.categoryList);
        console.log("categoryList", this.categoryList);
      } else {
        this.$message.error(res.message);
      }
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getGenericGoodsTypeList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getGenericGoodsTypeList();
    },
    close() {
      this.showDialog = false;
      this.addForm = {
        name: "", // 名称
        code: "", //	排序
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      if (this.title === "新增") {
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            const res = await addGenericGoodsType({
              ...this.addForm
            });
            if (res.code === 200) {
              this.$message.success("添加成功");
              this.getGenericGoodsTypeList();
              this.close();
            } else {
              this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      } else if (this.title === "编辑") {
        // 修改
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            const res = await updateGenericGoodsType({
              name: this.addForm.name,
              id: this.categoryId,
            });
            if (res.code === 200) {
              this.$message.success("修改成功");
              this.getGenericGoodsTypeList();
              this.close();
            } else {
              this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      }
    },
    async edit(row) {
      this.showDialog = true;
      this.title = "编辑";
      console.log(row);
      this.addForm.name = row.name;
      this.categoryId = row.id;
    },
    addFn() {
      this.showDialog = true;
      this.title = "新增";
    },
    async del(row) {
      const res = await deletetGenericGoodsType({ id: row.id });
      if (res.code === 200) {
        this.$message.success("删除成功");
        if (this.categoryList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getGenericGoodsTypeList();
      } else {
        // this.$message.error(res.message);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 20px;
}

.imgUpload {
  ::v-deep .el-upload:hover {
    border-color: #409eff;
  }

  ::v-deep .el-upload {
    position: relative;
    margin-bottom: 10px;
    margin-left: 5px;
    width: 70px;
    height: 70px;
    line-height: 30px;
    padding-top: 5px;
  }

  .txt {
    color: #595858;
    font-size: 12px;
  }
}

.el-upload--picture-card i {
  font-size: 12px !important;
}

.el-button--small {
  padding: 9px 0;
}

::deep el-button {
  text-align: center;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-form-item--small.el-form-item {
  margin-bottom: 26px !important;
}

::v-deep .el-icon-circle-close:before {
  color: #fff;
}
</style>
